﻿using MediatR;
using SheepStorage.API.Application.Command.DepartmentCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.Entity;
using SheepStorage.Service.Interface;

namespace SheepStorage.API.Application.CommandHandlers.DepartmentHandler
{
    public class DepartmentCreateCommandHandler : IRequestHandler<DepartmentCreateCommand, Results<int>>
    {
        private readonly IDepartmentService departmentService;

        public DepartmentCreateCommandHandler(IDepartmentService departmentService)
        {
            this.departmentService = departmentService;
        }

        /// <summary>
        /// 部门添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Results<int>> Handle(DepartmentCreateCommand request, CancellationToken cancellationToken)
        {
            var entity = new Department
            {
                // DepartmentId = request.DepartmentId,
                DepartmentName = request.DepartmentName,
                DepartmentTop = request.DepartmentTop,
                DepartmentIsUse = request.DepartmentIsUse,
                DepartmentCode = request.DepartmentCode,
                DepartmentType = request.DepartmentType,
                DepartmentArea = request.DepartmentArea,
                ImmediateSuperior = request.ImmediateSuperior,
                DepartmentManager = request.DepartmentManager,
                DepartmentDuty = request.DepartmentDuty,
                DepartmentMoney = request.DepartmentMoney,
                DepartmentCheck = request.DepartmentCheck,
                DutyManage = request.DutyManage,
                DepartmentParentId = 0
            };
            var list = await departmentService.AddDepartment(entity);
            return Results<int>.DataResult(list);
        }
    }
}
